package com.sdu.game.controller;


import com.sdu.game.mapper.ArticleMapper;
import com.sdu.game.mapper.CommentMapper;
import com.sdu.game.pojo.Article;
import com.sdu.game.pojo.ArticleExample;
import com.sdu.game.pojo.Comment;
import com.sdu.game.pojo.CommentExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin//告诉浏览器允许别的网站访问我
public class ArticleController {

    @Autowired
    ArticleMapper articleMapper;

    @Autowired
    CommentMapper commentMapper;

    @RequestMapping("/zhilang/v1/article/articleOut")
    public List<Article> articlesOut(){
        ArticleExample articleExample=new ArticleExample();
        articleExample.setOrderByClause("created_time asc");
        List<Article> list=articleMapper.selectByExample(articleExample);
        return  list;
    }

    //点击数增加
    @RequestMapping("/zhilang/v1/article/articleHist")
    //http://localhost:8080/zhilang/v1/article/articleHist?articleId=1
    public int articleHist(int articleId){
        //数据库修改帖子点击量
        ArticleExample articleExample=new ArticleExample();
        ArticleExample.Criteria criteria=articleExample.or();
        criteria.andArticleIdEqualTo(articleId);
        List<Article> list= articleMapper.selectByExample(articleExample);
        Article article=list.get(0);
        int hits=article.getHits()+1;
        article.setHits(hits);

        int updateRow=articleMapper.updateByPrimaryKeySelective(article);

        return updateRow;
    }



    //返回评论内容
    @RequestMapping("/zhilang/v1/article/commentOut")
    //http://localhost:8080/zhilang/v1/article/commentOut?articleId=1
    public List<Comment> commentOut(int articleId){
        //数据库查询该帖子id对应的所有评论内容
        //select * from comment where article_id=articleId
        CommentExample commentExample=new CommentExample();
        CommentExample.Criteria criteria=commentExample.or();
        criteria.andArticleIdEqualTo(articleId);
        List<Comment> list= commentMapper.selectByExample(commentExample);
        return list;
    }

    //增加评论
    @RequestMapping("/zhilang/v1/article/commentAdd")
    //http://localhost:8080/zhilang/v1/article/commentAdd?userId=1&articleId=1
    //http://localhost:8080/zhilang/v1/article/commentAdd?articleId=1&content=%E4%B8%8D%E5%A5%BD%E5%90%83&userId=1
    public void commentAdd(Integer userId,Integer articleId,String content){
        //前端获取评论相关信息

        //查找数据库中comment最大的id

        //创建评论
        Comment comment=new Comment();

        comment.setUserId(userId);
        comment.setArticleId(articleId);
        comment.setContent(content);

        //如果对象的属性值为空，不会给对应的列赋值
        int insertRow= commentMapper.insertSelective(comment);
        if (insertRow>=1){
            System.out.println("发布评论成功");
        }else {
            System.out.println("发布评论失败");
        }

    }





    //发帖
    @RequestMapping("/zhilang/v1/article/articleAdd")
    //http://localhost:8080/zhilang/v1/article/articleAdd?articleId=1&userId=1
    public void articleAdd(Integer userId,String title){
        //前端获取帖子相关信息

        int hits=0;
        Date creatTime=new Date();
//        String content="用java开发知狼小游戏";
        //查找数据库中article最大的id

        //创建文章
        Article article=new Article();

        article.setTitle(title);
        article.setHits(hits);
        article.setCreatedTime(creatTime);
//        article.setContent(content);

        //如果对象的属性值为空，不会给对应的列赋值
        int insertRow= articleMapper.insertSelective(article);
        if (insertRow>=1){
            System.out.println("发布帖子成功");
        }else {
            System.out.println("发布帖子失败");
        }
    }



}
